﻿#ifndef BLUETOOTHDEVICEMODEL_H
#define BLUETOOTHDEVICEMODEL_H

#include <QAbstractListModel>
#include "QtChCharset.h"
#include <QtBluetooth/QtBluetooth>
#include <QBluetoothDeviceInfo>


class BluetoothDeviceModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum EnumRowType{
        EnumRowType_Unkown,
        EnumRowType_Device,
        EnumRowType_Service,
    };
    Q_ENUMS(EnumRowType)
public:
    static BluetoothDeviceModel * Singleton();
public:
    enum BluetoothDeviceRole {
        ServiceNameRole = Qt::DisplayRole,
        SocketProtocolRole = Qt::UserRole,
        ServiceClassUuidRole,
        DeviceNameRole,
        DeviceAddrRole,
        SectionInfoRole,
        RowTypeRole,
        RssiRole,
        IsLowEnergyRole,
        IsPairedRole,
        IsSocketConnectedRole,
        childItemCountRole,
    };
    Q_ENUM(BluetoothDeviceRole)

    BluetoothDeviceModel(QObject *parent = nullptr);

    virtual int rowCount(const QModelIndex & = QModelIndex()) const override;
    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    virtual QHash<int, QByteArray> roleNames() const override;

    Q_INVOKABLE QVariantMap get(int row) const;
    // 增加
    Q_INVOKABLE void addService(const QString& serviceName, const int socketProtocol, const QString& serviceClassUuid, QBluetoothServiceInfo serviceInfo
                                , const QString& deviceName, const QString& deviceAddr, QBluetoothDeviceInfo deviceInfo);
    Q_INVOKABLE void addDevice(const QString& deviceName, const QString& deviceAddr, int rssi, bool isLowEnergy, bool isPaired, QBluetoothDeviceInfo deviceInfo);
    // 更新
    Q_INVOKABLE void updateDeviceName(const QString& deviceName, const QString& deviceAddr);
    Q_INVOKABLE void updateRssi(const QString& deviceAddr, int rssi);
    Q_INVOKABLE void updateSocketConnectState(const QString& deviceAddr, const QString& serviceClassUuid, bool isSocketConnected);
    // 删除行
    Q_INVOKABLE void remove(int row);
    // 清空
    Q_INVOKABLE void clear();
private:
    static QString toSectinInfo(const QString& deviceName, const QString& deviceAddr);
private:
    class BluetoothDevice
    {
    public:
        EnumRowType rowType;
        // 设备信息
        QString deviceName;
        QString deviceAddr;
        int rssi = 0;
        bool isLowEnergy = false;
        bool isPaired = false;
        QString sectionInfo;
        QBluetoothDeviceInfo deviceInfo;
        // 服务信息
        QString serviceName;
        int socketProtocol;
        QString serviceClassUuid;
        QBluetoothServiceInfo serviceInfo;
        // socket是否已经连接
        bool isSocketConnected = false;
        // 子项数
        int childItemCount = 0;
    public:
        BluetoothDevice():rowType(EnumRowType_Unkown) {}
        BluetoothDevice(const QString& deviceName, const QString& deviceAddr, int rssi, bool isLowEnergy, bool isPaired, QBluetoothDeviceInfo deviceInfo)
        {
            rowType = EnumRowType_Device;
            this->deviceName = deviceName;
            this->deviceAddr = deviceAddr;
            //this->sectionInfo = sectionInfo;
            this->rssi = rssi;
            this->isLowEnergy = isLowEnergy;
            this->isPaired = isPaired;
            this->deviceInfo = deviceInfo;
        }
        BluetoothDevice(const QString& serviceName, const int socketProtocol, const QString& serviceClassUuid, QBluetoothServiceInfo serviceInfo, const QString& deviceName, const QString& deviceAddr, QBluetoothDeviceInfo deviceInfo)
        {
            rowType = EnumRowType_Service;
            this->deviceName = deviceName;
            this->deviceAddr = deviceAddr;
            //this->rssi = rssi;
            //this->isLowEnergy = isLowEnergy;
            //this->isPaired = isPaired;
            //this->sectionInfo = sectionInfo;
            this->deviceInfo = deviceInfo;

            this->serviceName = serviceName;
            this->socketProtocol = socketProtocol;
            this->serviceClassUuid = serviceClassUuid;
            this->serviceInfo = serviceInfo;
        }
    };

    QList<BluetoothDevice> mBluetoothInfoList;
};

#endif // BLUETOOTHDEVICEMODEL_H
